#!/usr/bin/env python3

'''
main()
'''

from config import *
from gender import *
from gender_prediction_api import *
from multiprocessing import Pool
import pymongo
import random

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
#连接paper_program_data_info库
db_data_info = client[MONGO_DATA_INFO]

# '''
#     第二个多线程
# '''
# def do_use_gender_prediction_api(data_item):
#     gender_prediction_result = use_gender_prediction_api(data_item)
#
#     print("gender_prediction_result:",gender_prediction_result)
#
#     male = gender_prediction_result["male"]
#     if male >= 0.5:
#         print(111111111111)
#         db_data_info['dumplication_researcher_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":1}})
# #
#     else:
#         print(222222222222)
#         db_data_info['dumplication_researcher_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":0}})


'''
获取到所有researcher_sex_info信息，并调用gender_prediction接口
'''
def get_all_researcher_sex_info(follow):
    researcher_sex_info = db_data_info['dumplication_researcher_info'].find({}).skip(follow*100).limit(100)
    print("-----------------------------------------------:",follow)

    # demo = Pool(processes = 25)
    # demo.map(do_use_gender_prediction_api,researcher_sex_info)

    i = random.randint(0,39)
    j = 1
    for data_item in researcher_sex_info:
        gender_prediction_result = use_gender_prediction_api(data_item,i)
        print(str(follow) + "---------------------------------------------" +str(j))
        print("gender_prediction_result:",gender_prediction_result)

        male = gender_prediction_result["male"]
        j += 1
        if male >= 0.5:
            print(111111111111)
            db_data_info['dumplication_researcher_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":1}})
#
        else:
            print(222222222222)
            db_data_info['dumplication_researcher_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":0}})

    return None


'''
调用use_gender_prediction方法
'''
def use_gender_prediction_api(researcher_sex_info,i):
    if researcher_sex_info.get("AfN"):
        gender = gender_prediction(researcher_sex_info["AuN"],researcher_sex_info["AfN"],i)
    else:
        gender = gender_prediction(researcher_sex_info["AuN"],"",i)
    #print(researcher_sex_info["AuN"],researcher_sex_info["AfN"])
    gender_result = gender.use_gender_prediction()

    return gender_result

def main():
    #927571 262959
    #7000-8400
    follow_list = [i for i in range(9100,9277)]

    pool = Pool(processes = 22)
    pool.map(get_all_researcher_sex_info,follow_list)

    print("结束了2333333333")


if __name__ == '__main__':
    main()
